//<script language="Javascript">

<?

$_query_dummies [ "mod_img" ][ "list" ]	= "ID, (SELECT COUNT(*) FROM glue_object WHERE parenttype=".$_set->vars->objtype." AND objtype in(".$_set->vars->childtype.") AND parentid=mod_object.ID) as pdate, <%lang:mod_object.nameid%> as short";
$_query_dummies [ "mod_object" ][ "list" ]	= "ID, (SELECT COUNT(*) FROM glue_object WHERE parenttype=".$_set->vars->objtype." AND objtype in(".$_set->vars->childtype.") AND parentid=mod_object.ID) as pdate, <%lang:mod_object.nameid%> as short";
$_query_dummies [ "mod_calendar" ][ "list" ]	= "ID, date_format(date, '%d.%m %y') as pdate, <%lang:mod_calendar.textid%> as short";


//////////////////  ASSETS  //////////////////
if($_set->wtype == "win") {
//////////////////////////////////////////////

////////////////  OBJECT ASSETS (basic)
if($_tool[1] == "basic") {

////////////////// LIST GROUPS
	if($_set->action == "list"){ ?>

		ref.seed([
			tag("font", {style:"font-size:18px"}, "<?= ucfirst($_set->vars->descr)?>"), br, br,
			new List({id:ref.id+".lst", tool:'group.basic', descr:'<?=$_set->vars->boxdescr?>', new:'add <?= $DB->type_r( $_set->vars->objtype ) ?>', meta:ref.vars})
		]);
		
		$(ref.id+".lst").seed(<?= $_set->parse_list() ?>);
		

////////////////// EDIT GROUP OBJECT
	<? } else if($_set->action == "edit" || $_set->action == "edit.new"){ 
		
		if($_set->action == "edit.new") {
			$DB->query("INSERT INTO mod_object (ID, type, flog, active, creator) SELECT MAX(ID)+1, ".$_set->vars->objtype.", NOW(), 0, ".$_SESSION['uid']." FROM mod_object");
			$obj = $DB->get( $_set->vars->type, "!active order by ID desc" );
			?>
			ref.vars.id = <?=$obj['ID']?>;
			win.float.list["float_"+ref.vars.type+".<?=$obj['ID']?>"] = obj = {id:<?=$obj['ID']?>, type:'group', tool:'group.basic', name:"...", descr:"", objtype:ref.vars.objtype, list:[{ref:'name'},'...',0], meta:{objtype:ref.vars.objtype, img:''}};
			$(ref.vars.parent).vars.floatlist.push( ref.vars.id );
			
			
			$(ref.vars.parent+".tbody").insert({before: new win.trfloat(obj)});
			
			<?
		} 

		if( !$obj )
			 $obj	= $DB->get( $_set->vars->type, "ID=". $_set->vars->id, "*, <%lang:nameid%> as name, <%lang:mod_object.textid:long%> as descr" );

		$langs		= $Engine->text->filter( $obj['nameid'] );
		$longs		= $Engine->text->filter( $obj['textid'], "long" );

		function floatgroup( $id ) {
			global $DB, $_set;
			
			if( $list = $DB->reference( $_set->vars->type, "parentid=". $_set->vars->id, "media.img" ))
				foreach($list as $el) $group[] = "{id:$el, type:'img', tool:'media.img', name:\"$el\", descr:'img', meta:{ objtype:".$DB->type( 'media.img' ).", img:$el }}";
			
			return "[".(($group)?implode(",",$group):"")."]";
		}

		?>

		ref.seed([
			tag("font", {style:"font-size:18px"}, "Beheer <?= $DB->type_r( $_set->vars->objtype ); ?> "), tag("a", {id:ref.id+".h1", href:"http://<?=engine_get('control.public')?>/group&objid=<?=$obj['ID']?>", target:"_blank", style:"font-size:18px"}, "<?=$obj['name']?>"), " (view)", br, br,
			tag("input", {id:ref.id+".title"}), tag("div", {id:ref.id+".text"}),  br, tag("div", {id:ref.id+".objs"}),
			new Options({id:ref.id+".options", tool:'group.basic', descr:'Opties', floats: [
				new Shift({id:ref.id+".publish", type:'group', action:'shift', descr:'is public', tool:'group.shift', meta:{ value:<?=($obj['active']==1)?1:0;?>, options:[0,1], field:"active", objid:ref.vars.id}})
			], meta:{}})
		]);
		
		groups = {
			extra		: [ tag("div",{id:ref.id+".isactive"}) ]
		};
		
		[	{id:ref.id+".title",descr:'name group', direct:1, parenttype:"object", parentid:<?=$obj['ID']?>, textid:<?=$obj['nameid']?>, values:<?=($langs)?json_encode($langs):"{}"?>},
			{id:ref.id+".text", rich:1, ref:'basictxt', descr:'description', parenttype:"object", parentid:<?=$obj['ID']?>, direct:1<?=($obj['textid'])?",textid:".$obj['textid']:""?>, values:<?=($longs)?json_encode($longs):"{}"?>},
		].each(function(obj){ win.asset.txtfield.build(obj); });
		
		win.asset.dropbox.build({id:ref.id+".objs", descr:'Group children', tool:'media.img', type:'img', new:"add image", objtype:<?=$_set->vars->objtype?>, childtype:<?= $DB->type( "media.img" ) ?>, objid:<?=$obj['ID']?>, floats:<?=floatgroup($obj['ID']);?>});
		
		///// IMG BUTTON, CODE SHOULD BE IN DROPBOX CLASS
		Object.extend(loadcluster.init, {
			button_placeholder_id:ref.id+".objs.new.txt",
			post_params : { account :'<?=ACCOUNT?>', auth:<?=$Engine->user('ID')?>, objtype : <?=$DB->type( "media.img" )?>, parenttype:ref.vars.objtype , parentid:ref.vars.id },
			custom_settings : { float_target : ref.id+".objs", objtype : <?=$DB->type( "media.img" )?> }
		} );
		var swfu = new SWFUpload(loadcluster.init);
		
	<? }

////////////////  OBJECT ASSETS (basic)
} else if($_tool[1] == "calendar") {

////////////////// CALENDAR OBJECT

	if( $Request->action == "edit" || $Request->action == "edit.new" ){
	
	$Cal = ( $Request->action == "edit.new" )? new CALENDAR( array( "type"=>"cal.single", "otype"=>$Request->vars->parenttype, "objid"=>$Request->vars->objid ), 0, true ) : new CALENDAR( $Request->vars->type, (int) $Request->vars->id );
	$id = $Cal->id;
	
	if( $Request->action == "edit.new" ){
		?>

		win.float.list["float_"+ref.vars.type+".<?=$id?>"] = obj = {id:<?=$id?>, type:ref.vars.type, tool:'group.calendar', name:"new cal", descr:"...", objtype:ref.vars.objtype, list:['<?=date("d.m y")?>',{ref:'descr'}, {ref:'id'}], meta:{} };
		ref.vars.id = obj.id;

		//$(ref.vars.parent).vars.floatlist.push(<?=$id?>);
		//$(ref.vars.parent+".tbody").insert({before: new win.trfloat(obj)});
		
		<?
	}

	if( $Cal->get( 'nameid' ))		$name = $Cal->get( 'name' );
	else if( $Cal->get( 'objid' ))	$name = $Cal->object->get( 'name' );
	else							$name = "...";
	
	$format = "%d.%m %y";
	$langs	= $Engine->text->filter( $Cal->get( 'nameid' ));
	$longs	= $Engine->text->filter( $Cal->get( 'textid' ), "long" );

	$date	= explode(".", date("Y.n.j.G.i", strtotime( $Cal->get( 'date' ))));
	$location = $Engine->text->reference( "location", $Cal->get( 'room' ));

	?>
		ref.seed([
			tag("font", {style:"font-size:18px"}, "Beheer agendapunt"), " (<?= $format ?>, <?=$Cal->get( 'name' )?>)", br, br,
			tag("table",{cellpadding:"0", cellspacing:"0"},[
				tag("tr",0,[
					tag("td",0,[ tag("input",{id:ref.id+".publish.year", style:"width:108px;"}) ]),
					tag("td",0,[ tag("input",{id:ref.id+".publish.month", style:"width:58px;"}) ]),
					tag("td",0,[ tag("input",{id:ref.id+".publish.day", style:"width:58px;"}) ]),
					tag("td",0,[ tag("input",{id:ref.id+".publish.hour", style:"width:40px;"}) ]),
					tag("td",0,[ tag("input",{id:ref.id+".publish.minute", style:"width:40px;"}) ])
				])
			]),
			tag("div", {id:ref.id+".title"}), br,
			tag("div", {id:ref.id+".text"}), br,
			new Options({id:ref.id+".options", tool:'group.calendar', descr:'Opties', active:1, floats: [
				new Shift({id:ref.id+".publish", type:'calendar', action:'shift', descr:'is public', tool:'group.calendar', meta:{ value:<?=( $Cal->active())?1:0;?>, options:[0,1], field:"active", objid:ref.vars.id}}),
				new Opt({id:ref.id+".location", type:'object', action:'pull', descr:'locatie', value:"<?=$location?>", tool:'object.pull', meta: {
					parenttype:'calendar', parentfield:'room', parentid:ref.vars.id, objtype:<?=$DB->type("location")?>, field:"name", objid:<?=$Cal->get( 'room' )?>, flow:{limit:20}
				}}),
				new Shift({id:ref.id+".dayspan", type:'calendar', action:'shift', descr:'is span in days', tool:'group.calendar', meta:{ value:<?=($Cal->get( 'type' )==3)?0:1;?>, options:[3,4], field:"active", objid:ref.vars.id}}),
				<? if(ACCOUNT=="charlatan.tv") { ?>
				new Button({id:ref.id+'.facebook', type:'NONE', action:'NONE', descr:'add to Facebook', tool:'NONE', meta:{}})
				<? } ?>
			], meta:{}})
		]);
		
		[	{id:ref.id+".publish.year", descr:'Year', tool:"group.calendar.year", type:"calendar", meta: {value:<?=$date[0]?>, objid:<?=$id?>}},
			{id:ref.id+".publish.month", descr:'month', tool:"group.calendar.month", type:"calendar", meta: {value:<?=$date[1]?>, objid:<?=$id?>}},
			{id:ref.id+".publish.day", descr:'day', tool:"group.calendar.day", type:"calendar", meta: {value:<?=$date[2]?>, objid:<?=$id?>}},
			{id:ref.id+".publish.hour", descr:'hour', tool:"group.calendar.hour", type:"calendar", meta: {value:<?=$date[3]?>, objid:<?=$id?>}},
			{id:ref.id+".publish.minute", descr:'minutes', tool:"group.calendar.minute", type:"calendar", meta: {value:<?=($date[4]==00)?0:$date[4]?>, objid:<?=$id?>}}
		].each(function(obj){ win.asset.ajxfield.build(obj); });
		
		<? if(!$Cal->get( 'objid' )) { ?> win.asset.txtfield.build( {id:ref.id+".title", descr:'titel', parenttype:"calendar", parentid:<?=$id?>, direct:1<?=($Cal->get( 'nameid' ))?",nameid:".$Cal->get( 'textid' ):""?>, values:<?=($langs)?json_encode($langs):"{}"?>} ); <? } ?>
		win.asset.txtfield.build( {id:ref.id+".text", rich:1, ref:'basictxt', descr:'description', parenttype:"calendar", parentid:<?=$id?>, direct:1<?=($Cal->get( 'textid' ))?",textid:".$Cal->get( 'textid' ):""?>, values:<?=($longs)?json_encode($longs):"{}"?>} );

	<? }
}

/////////////////   PUSH   //////////////////
/////////////////////////////////////////////
} else if($_set->wtype == "push") {
	// CALENDAR NODE
	if($_set->vars->type == "cnode"){ 
		$DB->query("UPDATE txtdump SET active=0 WHERE textid IN (SELECT textid FROM mod_calendar WHERE ID=".$_set->vars->id." and textid>0)", 0 );
		$DB->query("DELETE FROM mod_calendar WHERE ID=".$_set->vars->id." LIMIT 1", 0 );
		$comment = "node removed!";
		?>
		xhttp.gfx("<?=$comment?>", ref.id);
	<? }
	
	
	/////////// LIST CONTROL
	if($_set->action == "list") { ?>

		$(ref.trg).seed(<?= $_set->parse_list() ?>);
	
	<? exit(); }

	if( strstr( $_set->action, "flow") ) { ?>
	
		$('control_list')._push(ref.trg, <?= $_set->flow() ?> );

	<? exit(); }

	
	////////////////// DUMP ELEMENT
	if($_set->action == "kill") {
	if($_set->vars->type == "calendar") {
		if( (int) $DB->get_key( "cal.single", "ID=". $_set->vars->id, "active" )) {
			$DB->query("UPDATE mod_calendar SET active=0 WHERE ID=".$_set->vars->id, 0 );
			$comment = $_set->vars->name." disabled to dump";
		} else {
			$DB->query("UPDATE txtdump SET active=0 WHERE textid IN (SELECT nameid, textid FROM mod_calendar WHERE ID=".$_set->vars->id.")");
			$DB->query("DELETE FROM glue_object WHERE (parenttype = ".$_set->vars->objtype." AND parentid =".$_set->vars->id.") OR (objtype = ".$_set->vars->objtype." AND objid =".$_set->vars->id.")");
			$DB->query("DELETE FROM mod_calendar WHERE active=0 AND ID=".$_set->vars->id." LIMIT 1");
			$comment = $_set->vars->name." removed!";
		}
		?> xhttp.gfx("<?=$comment?>", "listfloat.flow"); <?
			
	} else {
		if( (int) $DB->get_key( $_set->vars->type, "ID=". $_set->vars->id, "active" )) {
			$DB->query("UPDATE mod_object SET active=0 WHERE ID=".$_set->vars->id);
			$comment = $_set->vars->name." disabled to dump";
		} else {
			$DB->query("UPDATE txtdump SET active=0 WHERE textid IN (SELECT nameid, textid FROM mod_object WHERE ID=".$_set->vars->id.")");
			$DB->query("UPDATE db_media SET type=8 WHERE ID in (select objid from glue_object WHERE (parenttype = 1 AND parentid =".$_set->vars->id."))");
			$DB->query("DELETE FROM glue_object WHERE (parenttype = ".$_set->vars->objtype." AND parentid =".$_set->vars->id.") OR (objtype = ".$_set->vars->objtype." AND objid =".$_set->vars->id.")");
			$DB->query("DELETE FROM mod_object WHERE active=0 AND ID=".$_set->vars->id." LIMIT 1");
			$comment = $_set->vars->name." removed!";
		}
		?> xhttp.gfx("<?=$comment?>", "listfloat.flow"); <?

	}
	exit(); }
	
	////////////////// CALENDAR DASHBOARD EXTEND
	if($_tool[1] == "cal") {
		
		if($_set->action == "seed") {
		
			$hook = strtotime(date("Y-m-01") . (($_set->vars->hook<0)?"":"+") . $_set->vars->hook . " month");
			$diff = date("N", $hook)-1;
			$date = getdate($hook);
			
			if($group = $DB->query( "select distinct unix_timestamp(date(date)) as day from mod_calendar where datediff(date, from_unixtime($hook)) > -7 && active order by day" ))
				while( $row = mysql_fetch_array( $group, MYSQL_ASSOC)) $dates[] = (int) $row['day'];
			
			?>
			$(ref.trg).seed(<?=json_encode($dates)?>,{ year:<?=$date["year"]?>, month:<?=$date["mon"]-1?>, day:<?=$date["mday"]-$diff?>, monthstring:"<?=$date["month"]?>" });
			<?			
		} else if($_set->action == "day") {
		
			$hook = $_set->vars->uts;
			
			if( $result = $DB->filter( "cal.single", "unix_timestamp(date(date)) == $hook order by date", "ID, <%lang:mod_calendar.nameid%> as name, date" ))
				 while( $group[] = mysql_fetch_array( $result, MYSQL_ASSOC));
			else $group = array();
			
			?>
			$(ref.trg).day(<?=json_encode($group)?>,{ date:"<?=date("D, F jS  Y", $hook)?>" });
			<?			
		}


	exit(); }
}

////////////////// AJAX (direct) UPDATE FUNCTIONALITIES
if($_set->action == "ajax") {
	
	if(!$_set->vars->meta->field) {
		if($_tool[1]=="calendar") {
			$_set->vars->type = "cal.single"
			;
			$ref = $DB->get( "cal.single", "ID=".$_set->vars->meta->objid, "date, ".$_tool[2]."(date) as itv" );
			$date = date_create( $ref['date'] );
			$date->modify( ($_set->vars->meta->value - $ref['itv'])." ".$_tool[2]);
			
			$_set->vars->meta->value = $date->format("Y-m-d H:i:00");
			$_set->vars->meta->field = "date";
		}
	}
	
	$comment = ( $_set->ajax() )? "updated option":"warning: failed";
	
	?>
	xhttp.gfx("<?=$comment?>", ref.id);
	<?
	
exit(); }

////////////////// SHIFT FUNCTIONALITIES
if($_set->action == "shift") {

	$comment = ( $_set->shift() )? "option modified":"warning: failed";

	?>
	xhttp.gfx("<?=$comment?>", ref.id );
	<?
	
exit(); }


